﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class Admin_Hangsx : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            {
                Response.Redirect("/Online/Dangnhap.aspx");
            }
            else
            {
                var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý hãng sản xuất" select cn.Machucnang).FirstOrDefault();
                var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
                if (kt == 1)
                {
                    txtten.Focus();
                    LoadGV();
                    if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["m"])))
                    {
                        btnThem.Enabled = true;
                        btnSua.Enabled = false;
                    }
                    else
                    {
                        btnThem.Enabled = false;
                        btnSua.Enabled = true;
                        int ma = int.Parse(Request.QueryString["m"]);
                        var x = (from v in da.HangSXes where v.Mahang == ma select v.Tenhang).FirstOrDefault();
                        txtten.Text = x;
                    }
                }
                else
                {
                    Response.Redirect("/Online/Admin/Default.aspx");
                }
            }
        }
    }

    private void LoadGV()
    {
        var load = from a in da.HangSXes orderby a.Tenhang select a;
        gvHSx.DataSource = load;
        gvHSx.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        if (txtten.Text.Trim() == "")
        { tb.Text = "Nhập hãng sản xuất"; }
        else
        {
            HangSX h = new HangSX();
            h.Tenhang = txtten.Text.Trim();
            da.HangSXes.InsertOnSubmit(h);
            da.SubmitChanges();
            LoadGV();
            txtten.Text = "";
            txtten.Focus();
            tb.Text = "Thêm thành công";
        }

    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        int m = int.Parse(Request.QueryString["m"]);

        var update = (from a in da.HangSXes where a.Mahang == m select a).FirstOrDefault();
        update.Tenhang = txtten.Text.Trim();
        da.SubmitChanges();
        LoadGV();
        txtten.Text = "";
        txtten.Focus();
        tb.Text = "Cập nhật thành công !";
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < gvHSx.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)gvHSx.Rows[i].Cells[0].FindControl("ckq1");
            if (ck != null)
            {
                if (ck.Checked == true)
                {
                    Label lb = (Label)gvHSx.Rows[i].Cells[1].FindControl("Ma");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; int a;
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = int.Parse(mang[i]);
                var xoa = da.HangSXes.SingleOrDefault(p => p.Mahang == a);
                if (xoa != null)
                {
                    da.HangSXes.DeleteOnSubmit(xoa);
                    da.SubmitChanges();
                }
            }
            tb.Text = "Xóa thành công!";
            LoadGV();
        }
        else
            tb.Text = "Không bản ghi nào được chọn để xóa!";
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Online/Admin/Hangsx.aspx");
    }
}
